package com.example.s3demo.utils

object XmlUtil {
    fun buildXml(data: HashMap<String, Any>): String {
        val xmlBuilder = StringBuilder()

        xmlBuilder.append("<library>")
        for ((key, value) in data) {
            xmlBuilder.append("<$key>")
            when (value) {
                is String -> xmlBuilder.append(value)
                is ArrayList<*> -> {
                    for (item in value) {
                        if (item is HashMap<*, *>) {
                            xmlBuilder.append(buildXml(item as HashMap<String, Any>))
                        }
                    }
                }
                is HashMap<*, *> -> xmlBuilder.append(buildXml(value as HashMap<String, Any>))
            }
            xmlBuilder.append("</$key>")
        }
        xmlBuilder.append("</library>")

        return xmlBuilder.toString()
    }
}